package com.sk.extension.listener;

import com.rabbitmq.client.Channel;
import com.sk.extension.constants.Constant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AckListener {

    //正常处理
    @RabbitListener(queues = Constant.ACK_QUEUE)
    public void HandleMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //消费者逻辑
        System.out.printf("接收到消息: %s, deliveryTag: %d%n", new String(message.getBody(),"UTF-8"),
                message.getMessageProperties().getDeliveryTag());

        //业务逻辑处理（Mock）
        System.out.println("业务逻辑处理");
        //手动确认
//        channel.basicAck(deliveryTag,false);
    }

    //异常处理
//    @RabbitListener(queues = Constant.ACK_QUEUE)
//    public void HandleMessage(Message message, Channel channel) throws UnsupportedEncodingException {
//        //消费者逻辑
//        System.out.printf("接收到消息: %s, deliveryTag: %d%n", new String(message.getBody(),"UTF-8"),
//                message.getMessageProperties().getDeliveryTag());
//
//        //业务逻辑处理（Mock）
//        //模拟处理失败
//        int num = 3/0;
//        System.out.println("处理完成");
//    }
}
